package com.like.banzipao.server.util.roomstate

import com.like.banzipao.server.model.Poker
import com.like.banzipao.server.model.Room
import com.like.banzipao.server.model.User
import com.like.banzipao.server.util.roomstate.playroomstate.OneVsOneRoomState
import com.like.banzipao.server.util.roomstate.playroomstate.OneVsThreeRoomState
import com.like.banzipao.server.util.roomstate.playroomstate.TwoVsTwoRoomState

class RoomManager(private val room: Room) {
    private val readyRoomState: IRoomState by lazy {
        ReadyRoomState(room) {
            val onWin = {
                room.players.forEachIndexed { index, player ->
                    if (player != null) {
                        player.integralChange = playRoomState?.getIntegralChange(player) ?: 0
                    }
                }
            }
            playRoomState = when (it) {
                BaoType.NONE -> TwoVsTwoRoomState(room, onWin)
                BaoType.BAO -> OneVsThreeRoomState(room, it, onWin)
                BaoType.FAN_BAO -> OneVsOneRoomState(room, it, onWin)
                BaoType.DRAW_BAO -> OneVsThreeRoomState(room, it, onWin)
                BaoType.DRAW_FAN_BAO -> OneVsOneRoomState(room, it, onWin)
            }
        }
    }
    private var playRoomState: IRoomState? = null

    fun contains(user: User): Boolean {
        return readyRoomState.contains(user)
    }

    fun contains(userId: Int): Boolean {
        return readyRoomState.contains(userId)
    }

    fun isFull(): Boolean {
        return readyRoomState.isFull()
    }

    fun isEmpty(): Boolean {
        return readyRoomState.isEmpty()
    }

    fun addPlayer(user: User) {
        readyRoomState.addPlayer(user)
    }

    fun removePlayer(user: User) {
        readyRoomState.removePlayer(user)
    }

    fun ready(user: User) {
        readyRoomState.ready(user, playRoomState?.getDealerUserId() ?: room.players.first()!!.user.id)
    }

    fun again(user: User) {
        room.reset(user)
        readyRoomState.again(user)
        playRoomState = null
    }

    fun bao(user: User) {
        readyRoomState.bao(user)
    }

    fun fanBao(user: User) {
        readyRoomState.fanBao(user)
    }

    fun baoPass(user: User) {
        readyRoomState.baoPass(user)
    }

    fun fanBaoPass(user: User) {
        readyRoomState.fanBaoPass(user)
    }

    fun getAllFindFriendPokers(): List<Poker> {
        return readyRoomState.getAllFindFriendPokers()
    }

    fun findFriend(user: User, id: Int) {
        readyRoomState.findFriend(user, id)
    }

    fun drawPlayed(user: User, ids: List<Int>) {
        readyRoomState.drawPlayed(user, ids)
    }

    fun drawTake(user: User, ids: List<Int>) {
        readyRoomState.drawTake(user, ids)
    }

    fun getSpade7(): Poker? {
        return readyRoomState.getSpade7()
    }

    fun playPlayed(user: User, ids: List<Int>): Boolean {
        return playRoomState?.playPlayed(user, ids) ?: false
    }

    fun playPass(user: User) {
        playRoomState?.playPass(user)
    }

}